package com.db.compare.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseResponse;
import com.db.compare.model.RbacUserRole;
import com.db.compare.mapper.RbacUserRoleMapper;
import com.db.compare.service.RbacUserRoleService;
import com.db.compare.vo.RbacUserRoleVo;
import com.db.compare.exp.RbacUserRoleExp;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.db.compare.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONObject;


/**
* 用户角色关系
* */
@Slf4j
@Controller
@RequestMapping("/rbac/user/role")
public class RbacUserRoleController extends BaseController{

    @Autowired
    private RbacUserRoleMapper rbacuserroleMapper;
    @Autowired
    private RbacUserRoleService rbacuserroleService;

    @RequestMapping(value={"/list",""})
    public String rbacuserroleList(){
        return "/html/RbacUserRole";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<RbacUserRole> pages(Model model,@RequestParam Map rbacuserroleMap){
        RbacUserRoleVo rbacuserroleVo = JSON.parseObject(JSON.toJSONString(rbacuserroleMap), RbacUserRoleVo.class);
        return rbacuserroleService.pageInfo(rbacuserroleVo);
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        rbacuserroleMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody RbacUserRole rbacuserrole){
        rbacuserrole.setUpdateTime(new Date());
        if(rbacuserrole.getId()==null) {
            rbacuserrole.setCreateTime(new Date());
            rbacuserroleMapper.insert(rbacuserrole);
        }else{
            rbacuserroleMapper.updateByPrimaryKey(rbacuserrole);
        }
        return new BaseResponse(200,rbacuserrole);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        RbacUserRole rbacuserrole = rbacuserroleMapper.selectByPrimaryKey(pk);
        return new BaseResponse(200,rbacuserrole);
    }
    //下载模板
    @RequestMapping("/download")
    public void testRespExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String ids=request.getParameter("ids");
        rbacuserroleService.download(ids,response);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public BaseResponse upload(@RequestParam("file") MultipartFile file){
        if(!rbacuserroleService.upload(file)){
            return buildFailResponse();
        }

        return buildSuccessResponse();
    }
}